<?php

namespace plugin\webman\xingcunzhe;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;

class Common
{

    // 计算杀手去哪个房间
    static function killer(){
        $expect = Common::getnowexpect();
        // $expect = '202312050879';                //测试待删除
        // $room['killer_to_smallroomid'] = (int)1;
        $key = 'killer_'.$expect;
        $room['killer_to_smallroomid'] = Redis::get($key);

        // $room['killer_to_smallroomid'] = false;   //测试待删除


        if(!$room['killer_to_smallroomid']){
            $room['killer_to_smallroomid'] = rand(1,8);  //开出的房间号    需要更新为控制开奖
            Redis::set($key,$room['killer_to_smallroomid']); 
            $record = [
                'samllroomid'=> $room['killer_to_smallroomid'],
                'roomname'=>Common::smallroomname($room['killer_to_smallroomid']),
                'expect'=>$expect,
                'addtime'=>date('Y-m-d H:i:s',time())
            ];
            Db::table("wa_killer_record")->insert($record);

            // 把每个投注记录 改掉杀手的结果
            $touzhus = Db::table("wa_touzhu")->where(['expect'=>$expect])->get()->map(function ($value) {return (array)$value;})
            ->toArray();
           
            if($touzhus){
                // 把投注订单的状态改掉
                $handle = Common::handlekiller($touzhus,$expect,$room['killer_to_smallroomid']);
            }
            // 发放瓜分的收益
            // 查询本期 所有投注的人

            // $touzhus = Db::table("wa_touzhu")->where(['expect'=>$expect])->sum("coin"); //本期投注所有金额
            $guafen1 = (int)Db::table("wa_touzhu")->where(['expect'=>$expect,'tzid'=>$room['killer_to_smallroomid'],'ifyouxiao'=>1])->sum("coin"); //本期 杀手 瓜分金额
           
            // 销毁池 和 奖金池
            // $guafen1 = 1000;
            $config= self::getbaseconfig();
            $xiaohuirate = 5;
            $nengliangrate = 5;

            // 循环遍历三个房间 写入奖池记录表
            $jiangchi_all = 0;
            
            for($i =2;$i<=4;$i++){
                $zongliang= (int)Db::table("wa_touzhu")->where(['expect'=>$expect,'roomid'=>$i,'ifyouxiao'=>1])->sum("coin"); 
                $xiaohui = $zongliang*$xiaohuirate*0.01; // 销毁池
                $nengliang = $zongliang*$nengliangrate*0.01; // 能量池
                // var_dump($zongliang,$xiaohui,$nengliang);die;
                // 转入股东账号
                switch ($i) {
                    case '0':
                        $bizhong = 'eso';
                        break;
                    case '2':
                        $bizhong = 'eso';
                        break;  
                    case '3':
                        $bizhong = 'cn';
                        break;
                    case '4':
                        $bizhong = 'es';
                        break;          
                }

                $jiangchi = [
                    'xiaohui'=>$xiaohui,
                    'nengliang'=>$nengliang,
                    'addtime'=>time(),
                    'roomid'=>$i
                ];
                $in =   Db::table("wa_jiangchi")->insert($jiangchi);
                    // 扣完销毁和能量的数据
                
                //本期 杀手 瓜分金额
                $beishaall =  (int)Db::table("wa_touzhu")->where(['expect'=>$expect,'tzid'=>$room['killer_to_smallroomid'],'ifyouxiao'=>1,'roomid'=>$i])->sum("coin"); 
                $othersall = $zongliang - $beishaall;   
                if($othersall==0){
                    $othersall = 1;
                }
               
              $bili = $beishaall/$othersall;
                //查询本房间所有真人投注信息   
              $tzlist = Db::table("wa_touzhu")->where(['expect'=>$expect,'isrobot'=>0,'roomid'=>$i])->get()->map(function ($value) {return (array)$value;})
                ->toArray();
                 // var_dump($tzlist);
                if($tzlist){
                    foreach ($tzlist as $kt =>$vt){
                        if($vt['status']==1){
                            // 中奖
                            $pingjun = round($bili * $vt['coin'],4);
                            var_dump('平均额');
                            var_dump($pingjun);
                            // $fanhuan = $pingjun +$vt['coin'];
                            Db::table("wa_touzhu")->where(['id'=>$vt['id']])->update(['rewards'=>$pingjun]);
                        }
                    }
                }
              

            }
           
        }
        
        // 得改为每个房间的开奖

        $room['expect']=$expect;
        $room['tzcoin'] = (int)Db::table("wa_touzhu")->where(['expect'=>$expect,'tzid'=>$room['killer_to_smallroomid']])->sum("coin");
        return $room;
    }

    // 处理开奖事宜
    static function handlekiller($list,$expect,$roomid){
    //    var_dump($list);die;
        $roomname = Common::smallroomname($roomid);
        foreach ($list as $k =>$v){
            // var_dump($v);
            if($v['tzid']!=$roomid){
                $up = Db::table("wa_touzhu")->where(['id'=>$v['id'],'ifyouxiao'=>1])->update(['resultid'=>$roomid,'resultname'=>$roomname,'status'=>1]);
            }else{
                $up = Db::table("wa_touzhu")->where(['id'=>$v['id'],'ifyouxiao'=>1])->update(['resultid'=>$roomid,'resultname'=>$roomname,'status'=>2]);
            }

        }
    }

    static function checktoken($client_id,$uid,$token){
       $user = Db::table('wa_users')->where("id",$uid)->value('token');
    //    var_dump($user,$token);
       if(!$user){
            $data=[
                'code'=>10020,
                'msg'=>'用户不存在',
                'info'=>''
            ];
            Gateway::sendToClient($client_id,Common::returndata('usernotexit',$data));
            exit();
            return 10020;
       }
       if($user && $user!= $token){
        $data=[
            'code'=>700,
            'msg'=>'您的登录已过期，请重新登录',
            'info'=>''
        ];
            Gateway::sendToClient($client_id,Common::returndata('tokenexpire',$data));
            exit();
            return 700;
       }
       return 1;

    }
    static function checktoken2($client_id,$uid,$token){
        $user = Db::table('wa_users')->where("id",$uid)->value('token');
        // var_dump($user,$token);
        if(!$user){
             $data=[
                 'code'=>10020,
                 'msg'=>'用户不存在',
                 'info'=>''
             ];
             return 10020;
        }
        return 1;
 
     }

    static function returndata($action,$data)
    {
        $returndata=[
            'action'=>$action,
            'data'=>$data
        ];
       return json_encode($returndata,JSON_UNESCAPED_SLASHES);
    }

    // 获取开启的支付方式
    static function getpaytypelist(){
        $config = Db::table("wa_options")->where("name","system_config")->select("value")->get();
        $config= json_decode($config,1);
        $config = json_decode($config[0]['value'],1);

        $data['alipay']=$config['menu']['alipaystatus'];
        $data['wxpay']=$config['menu']['wxpaystatus'];
        $data['usdt']=$config['menu']['usdtstatus'];
        return $data;
    }
    static function getbaseconfig(){
        $config = Db::table("wa_options")->where("name","system_config")->select("value")->get();
        $config= json_decode($config,1);
        $config = json_decode($config[0]['value'],1);
        return $config;
    }

    // 获取用户信息
    static function getuserinfo($uid){
        $ifexit = Db::table("wa_users")->where("id",$uid)->first();
        // var_dump('这个');
        // var_dump($ifexit,$uid);
        $user['id']=$ifexit->id;
        $user['username']=$ifexit->username;
        $user['nickname']=$ifexit->nickname;
        $user['coin']=$ifexit->score;
        $user['money']=$ifexit->money;
        $user['isrobot']=$ifexit->isrobot;
        $user['status']=$ifexit->status;
        $user['roomid']=$ifexit->roomid;
        
        return $user;
    }

    // 更新期号投注信息
    static function upexpect($roomid,$expect,$gezi=''){
        $data = Db::table("wa_xcz_expect")->where(["expect"=>$expect,"roomid"=>$roomid])->first();
        $list = $data->numlist;

        $datagezi = explode(',',$list);
  
        $datagezi[$gezi]=$datagezi[$gezi]+1;

        $numlist= implode(',',$datagezi);
        $data = Db::table("wa_xcz_expect")->where(["expect"=>$expect,"roomid"=>$roomid])->update(['numlist'=>$numlist]);
        $data2 = Db::table("wa_xcz_expect")->where(["expect"=>$expect,"roomid"=>$roomid])->first();
        $data2->numlist = explode(',',$data2->numlist);

        return  $data2;
    }
    static function getexpectinfo($roomid,$expect){
        $data = Db::table("wa_xcz_expect")->where(["expect"=>$expect,"roomid"=>$roomid])->first();

        $list = $data->numlist;

        $datagezi = explode(',',$list);
        $data->numlist = $datagezi;
        return  $data;
    }

    // 获取几号房间 几个楼层内小人列表
    static function getuserlist($roomid,$level){
        
        switch ($roomid) {
            case '2':
                $i = 4;   //有几个格子
                break;
            case '3':
                $i = 3;
                break;
            case '4':
                $i = 2;
                break;            
        }

        for($j=0;$j<=$i;$j++){
            $a['gezi']=$j;
            $a['count']=Db::table("wa_xcz_position")->where(["roomid"=>$roomid,"position"=>$level,"gezi"=>$j])->count();
            $b=Db::table("wa_xcz_position")->where(["roomid"=>$roomid,"position"=>$level,"gezi"=>$j])->get()->map(function ($value) {return (array)$value;})
            ->toArray();
            // var_dump($b);die;
            // $a['list']
            if($b){
                foreach($b as $k=>$v){
                    $v['userinfo']=Common::getuserinfo($v['uid']);
                    $c[]=$v;
                }
                $a['list']=$c;
            }else{
                $a['list']=[];
            }
            
            $data[]=$a;
        }
        return $data;
    }
  
    static function handleuserlist($roomid,$level){

    }


    



    // 更新用户当前位置信息
    static function upuserpostion($uid,$roomid,$level,$gezi){
        $ifexit=Db::table("wa_xcz_position")->where(["uid"=>$uid])->first();
        if($ifexit){
            Db::table("wa_xcz_position")->where(["uid"=>$uid])->update(['roomid'=>$roomid,'position'=>$level,'gezi'=>$gezi]);
        }else{
            Db::table("wa_xcz_position")->where(["uid"=>$uid])->insert(['uid'=>$uid,'roomid'=>$roomid,'position'=>$level,'gezi'=>$gezi]);
        }
    }


    // 记录用户金币日志
    static function usercoin_record($data){
        $in = Db::table("wa_usercoin_record")->insert($data);
        if($in){
            return 1;
        }else{
            return 0;
        }
    }
    // 生成新的一期
    static function createnewexpect($nowexpect){
        $arr = [2,3,4];
        $num = ['0,0,0,0,0','0,0,0,0','0,0,0'];
        $ifexit  = Db::table("wa_xcz_expect")->where(['expect'=>$nowexpect])->first();
        if(!$ifexit){
            foreach ($arr as $k => $v) {
                $data =[
                    'roomid'=>$v,
                    'expect'=>$nowexpect,
                    'nowlevel'=>0,
                    'numlist'=>$num[$k],
                ];
                
               $id =  Db::table("wa_xcz_expect")->insertGetId($data);
               if(!$id){
                    $broadcast = [            
                        'code'=>1095,
                        'msg'=>'新期号创建失败',
                        'info'=>[]
                    ];
                    
                    Gateway::sendToAll(Common::returndata('upusernums',$broadcast));
               }
            }
        }
        
    }

    // 开奖 分配中奖金额
    static function kaijiang($nowexpect,$roomid,$level,$killedgezi){

        var_dump("开奖");

        $list = Db::table("wa_xcz_touzhu")->where(["expect"=>$nowexpect,'level'=>$level,'roomid'=>$roomid,'ifwin'=>2])->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        // var_dump($list);
        foreach ($list as $k => $v) {

            if($v['gezi']==$killedgezi){
                // 失败
                Db::table("wa_xcz_touzhu")->where("id",$v['id'])->update(['ifwin'=>0,'result'=>$killedgezi]);
                $uppostion=Db::table("wa_xcz_position")->where(["uid"=>$v['uid']])->update(["position"=>0,'gezi'=>0]);
                var_dump($v['uid']."失败,位置更新情况".$uppostion);
            }else{
                
                // 中奖金额
                $peilvinfo = Db::table("wa_xcz_rule")->where(['roomid'=>$v['roomid'],'level'=>$v['level']])->first();
                $rewards = $peilvinfo->peilv * $v['coin'];
                
                $up =  Db::table("wa_xcz_touzhu")->where("id",$v['id'])->update(['ifwin'=>1,'result'=>$killedgezi,'rewards'=>$rewards]);
                $userposition = Db::table("wa_xcz_position")->where(["uid"=>$v['uid']])->first();

                // var_dump($userposition);


                if($userposition){
                    $newposition = $userposition->position + 1;
                }else{
                    $newposition=0;
                }
                // var_dump('新的楼层信息',$newposition);
                $uppostion = Db::table("wa_xcz_position")->where(["uid"=>$v['uid']])->update(["position"=>$newposition]);
                var_dump($v['uid']."躲避成功,位置更新情况".$uppostion);
                // 更新用户余额
                $tuihuan = $rewards + $v['coin'];
             
                Common::updateusercoin($v['uid'],$tuihuan,1,"房间".$v['roomid']."楼层".$v['level'].'躲避成功，中奖'.$rewards.',并退还本金'.$v['coin']);
            }
           
        }
    }


    // 生成期号
    static function createexpect($client_id='',$roomid,$nowexpect){
    
        switch ($roomid) {
            case '2':
                $level = rand(1,10);
                $numslist = "0,0,0,0,0";
                break;
            case '3':
                $level = rand(1,7);
                $numslist = "0,0,0,0";
                break;
            case '4':
                $level = rand(1,4);
                $numslist = "0,0,0";
                break;           

        }
        $data =[
            'roomid'=>(int)$roomid,
            'expect'=>$nowexpect,
            'nowlevel'=>$level,
            'numlist'=>$numslist,
        ];
       $id =  Db::table("wa_xcz_expect")->insertGetId($data);
       if($id){
            return $data;
       }else{
            $res = [
                    'code'=>1089,
                    'msg'=>'期号生成失败',
                    'info'=>[]
            ];
            Common::returndata('returnenterroom',$res);
       }

    }

    // 获取房间每层的赔率信息
    static function geipeilvlist($roomid){
        $peilvlist = Db::table("wa_xcz_rule")->where(["roomid"=>$roomid])->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        return $peilvlist;
    }

    // 获取房间内玩家列表
    static function getplayerlist($roomid){
        $list = Db::table("wa_users")->where(["roomid"=>$roomid])->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($list){
            foreach($list as $k=>$v){
                $a['id']=$v['id'];
                $a['nickname']=$v['nickname'];
                $a['position']=Self::getuserposit($roomid,$v['id']);
                $new[]=$a;
            }
            return $new;
        }else{
            return [];
        }

    }

    // 获取用户当前房间投注金额
    static function getuserallbet($expect,$roomid){
        $num = (string)Db::table("wa_xcz_touzhu")->where(["expect"=>$expect,"roomid"=>$roomid])->sum("coin");
        return $num;
    }

    // 获取用户当前在第几层第几个格子
    static function getuserposit($roomid,$uid){
        $position = Db::table("wa_xcz_position")->where(["roomid"=>$roomid,"uid"=>$uid])->first();
        if(!$position){
            return [
                "uid"=>$uid,
                "position"=>"0",
                "gezi"=>"0",
            ];
        }else{
            $data =[
                "uid"=>$uid,
                "position"=>$position->position,
                "gezi"=>$position->gezi,
            ];
            return $data;
        }
    }


    // 操作用户金币   type : 0 减少  ；  1增加
    static function updateusercoin($uid,$coin,$type,$remark){
        // var_dump($uid);die;
        $userinfo = Common::getuserinfo($uid);
        if($type==0){
            $paytype = 0;
            $newcoin = $userinfo['coin']-$coin;
        }else{
            $paytype = 1;
            $newcoin = $userinfo['coin']+$coin;
        }
        
        $up = Db::table("wa_users")->where("id",$uid)->update(['score'=>$newcoin]);
        if($up){
            $data=[
                'uid'=>$uid,
                'coin'=>$coin,
                'type'=>$paytype,
                'remark'=>$remark,
                'addtime'=>date('Y-m-d H:i:s',time()),
            ];
           $record =  Db::table("wa_usercoin_record")->insert($data);
            return 1;
        }else{
            return 0;
        }
    
    }

   // 转入股东账号
    static function transgudong($type,$coin,$bizhong){
        // 能量池/奖金池    金额    币种   加
        $config =self::getbaseconfig();
        if($type == 1 ){
            $account = $config['logo']['nengliangaccount'];
        }else{
            $account = $config['logo']['xiaohuiaccount'];
        }
        $url = $config['logo']['jiekouurl'].'setWalletValue?username='.$account.'&bizhong='.$bizhong.'&type=1&amount='.$coin;
        file_get_contents($url);
        
    }


    // 更新人员小房间位置信息
    static function upposition($uid,$smallroomid){
        $expect = Common::getnowexpect();
        $key = "position_".$uid; 
        Redis::set($key, $smallroomid);
    }

    static function robotmove(){
        // for($i=2;$i<5;$i++){
        //     $roomid = $i;
        //     $robots =  Db::table("wa_users")->where(['isrobot'=>1,'roomid'=>$roomid])->get()->toArray();
        
        //     $list_robots = Common::object_array($robots);
        //     $expect = Common::getnowexpect();
        //     // 每次挪动几个机器人
        //     $nums = rand(2,6);
        //     $arr = array_rand($list_robots,$nums);
        //     // var_dump($arr);die;
        //     foreach($arr as $keyt){
        //         $list[] = $list_robots[$keyt];
        //     }
        //     // var_dump($list);
        //     $list_robots = $list;


        //     foreach($list_robots as $k =>$v){
        //         // 记录机器人当前位置
        //         $key = "robotpositon_".$expect.'_'.$v['id'];
        //         $to['smallroomid']=rand(1,8);
        //         Redis::set($key,$to['smallroomid']);
        //         $to['uid']=(string)$v['id'];
            
        //             $res2=[
        //                 'code'=>1,
        //                 'msg'=>'用户投注挪动了小房间位置',
        //                 'info'=>$to
        //             ];               
        //         // 广播机器人位置变化信息
        //         Gateway::sendToGroup($roomid,Common::returndata('usertouzhu',$res2));
        //         // 机器人模拟投注   并广播房间内投注数据变化
        //         $config =self::getbaseconfig();
        //         // var_dump($config);die;
        //         $coin = rand($config['logo']['robotmin'],$config['logo']['robotmax']);
        //         $touzhu=[
        //             'uid'=>$v['id'],
        //             'expect'=>$expect,
        //             'tzid'=>$to['smallroomid'],
        //             'tzname'=>Common::smallroomname($to['smallroomid']),
        //             'coin'=>$coin,
        //             'addtime'=>date('Y-m-d H:i:s',time()),
        //             'status'=>0,
        //             'roomid'=>$roomid,
        //             'isrobot'=>1
        //         ];
        //         $iftouzhu = Db::table("wa_touzhu")->where(['expect'=>$expect,'uid'=>$v['id'],'roomid'=>$roomid])->first();
        //         if($iftouzhu){
        //             $del = Db::table("wa_touzhu")->where(['expect'=>$expect,'uid'=>$v['id'],'roomid'=>$roomid])->update(['tzid'=>$to['smallroomid'],'tzname'=>Common::smallroomname($to['smallroomid'])]);
                
        //         }else{
        //             $in = Db::table("wa_touzhu")->insert($touzhu);
        //         }
                    
                
                
        //         // 发送群组socket 更新房间内
        //         $res=[
        //             'code'=>1,
        //             'msg'=>'小房间投注总额更新',
        //             'info'=>Common::getroom_coin_list($roomid,$expect)
        //         ];
                
        //         Gateway::sendToGroup($roomid,Common::returndata('updatesmallcoin',$res));
        //     }
        // }
    }
    // 获取机器人位置信息
    static function getrobotposition($uid){
        $expect = Common::getnowexpect();
        $key = "robotpositon_".$expect.'_'.$uid;
    
        $p = Redis::get($key);
        if(!$p){
            return "0";
        }else{
            return $p;
        }
    }    

    // 获取人员房间位置信息
    static function getposition($uid){
        $expect = Common::getnowexpect();
        $key = "position_".$uid; 
        $p = Redis::get($key);
        if(!$p){
            return "0";
        }else{
            return $p;
        }
    }
    static function joinrobots($roomid){
        $robots =  Db::table("wa_users")->where(['isrobot'=>1,'roomid'=>$roomid])->get()->toArray();
        $list_robots = Common::object_array($robots);
        foreach($list_robots as $k=>$v){
            $v['client_id']="7f00000108ff00000003";
            $v['uid']=(string)$v['id'];
            $v['userinfo']=Common::getuserinfo($v['id']);
            // 增加每个人的位置信息
            $v['position']=Common::getrobotposition($v['id']);
            $new[]=$v;
        }
        return $new;
    }


    static  function object_array($array){
        if(is_object($array)){
          $array = (array)$array;
        }
        if(is_array($array)){
          foreach($array as $key=>$value){
            $array[$key] = self::object_array($value);
          }
        }
        return $array;
      }


    // 进入房间
    static function enterroom($client_id,$roomid,$userinfo){
        // 设备绑定UID
        $key = 'binduid_'.$client_id;  //绑定uid写入redis
        $useredis = Redis::set($key,$userinfo['id']);
        Gateway::bindUid($client_id,$userinfo['id']);
        // socket 加入群组
        Gateway::joinGroup($client_id, $roomid);
        // 更新用户所在房间
        self::changeuserroomid($userinfo['id'],$roomid);
        // 增加房间在线人数
        self::uproom_nums($roomid,1,1);

    }

    // 离开房间

    static function leaveroom($client_id,$roomid,$userinfo){

        // socket 离开群组
        Gateway::leaveGroup($client_id, $roomid);
        // 更新用户所在房间
        self::changeuserroomid($userinfo['id'],0);
        // 减少房间在线人数
        self::uproom_nums($roomid,0,1);

    }

    // 更新用户所在房间信息
    static function changeuserroomid($uid,$roomid){
       $change =  Db::table("wa_users")->where("id",$uid)->update(['roomid'=>$roomid]);
    //    var_dump($change);die;
    }


    // 获取当前房间人数
    static function getroom_nums($roomid){
        $key = "roomnums_".$roomid;  //房间人数key
        $nums = Redis::get($key);
        if(!$nums){
            Redis::set($key, 0);
            return 0;
        }
        return $nums;
    }

    // 获取当前时间的期号
    static function getnowexpect(){
        // $hour = date("H");
		// $min =date("i");
		// // $hour = 1;
		// // $min =15;
		// $a = $hour*60+$min;

		// if($a<1000 && $a>100){
		// 	$a='0'.$a;
		// }
		// if($a<100){
		// 	$a='00'.$a;
		// }
        // var_dump(date('Ymd').$a);
		// return date('Ymd').$a;

         
        $startTime = date("Y-m-d 00:00:00"); // 设置起始时间
        $a = self::calculatePeriod($startTime); // 调用函数获取当前期号
        if($a<1000 && $a>100){
			$a='0'.$a;
		}
		if($a<100){
			$a='00'.$a;
		}
        return date('Ymd').$a;
    }

    // 计算下一期的开始时间
    static function getnextexpecttime(){
        $startTime = date("Y-m-d 00:00:00"); // 设置起始时间
        $period = self::calculatePeriod($startTime); // 调用函数获取当前期号
        $nextPeriod = $period + 1; // 计算下一期期号
        $nexttime = $nextPeriod * 2 *60 + strtotime($startTime);
        // $nextStartTime = date('Y-m-d H:i:s',$nexttime);
        
        $julinextime = $nexttime-time();
        // var_dump($nexttime,time(),$julinextime);die;
        return $julinextime;
    }
    
    static function calculatePeriod($startTime) {
        $period = floor((time() - strtotime($startTime)) / (60 * 2)); // 将时间转换为分钟并除以2得到期号
        return $period;
    }
    // 根据旗号获取时间
    static function gettimebyexpect($expect){
		// var_dump($expect);die;
		// $date = date('Ymd');
		$a = substr($expect,8);
		$year = substr($expect,0,4);
		$month = substr($expect,4,2);
		$day = substr($expect,6,2);
		// var_dump($year,$month,$day);die;
		$date = $year.'/'.$month.'/'.$day;
		// var_dump($date);die;
		if($a[0]=="0"){
			$a = mb_substr($a,1);
		}
		$hour = intval(floor($a/60));
		$min = $a%60;
		$time  = strtotime($date.' '.$hour.':'.$min);
		
		// $time  = $date.' '.$hour.':'.$min;
		return date('Y/m/d H:i',$time);

	}

    // 获取当前房间内某一期的投注金额 小房间金额列表
    static function getroom_coin_list($roomid,$expect){
        for($i=0;$i<9;$i++){
            $room['smallroomid']=$i;
            $room['allcoin'] =  (string)Db::table("wa_touzhu")->where(['roomid'=>$roomid,'tzid'=>$i,'expect'=>$expect,'ifyouxiao'=>1])->sum("coin");
            $new[]=$room;
        }
        return $new;
    }

    // 增加房间在线人数  type:0 减少；  1增加；    num 执行数量
    static function uproom_nums($roomid,$type,$num){
        $key = "roomnums_".$roomid; 
        $nums = Redis::get($key);
        if($type==1){
            if(!$nums){
                Redis::set($key, 1);
                return 1;
            }else{
                $nums = $nums+$num;
                Redis::set($key, $nums);
                return $nums;
            }
        }else{
            if(!$nums){
                Redis::set($key, 0);
                return 1;
            }else{
                if($nums>$num){
                    $nums = $nums-$num;
                }else{
                    $nums = 0;
                }
                
                Redis::set($key, $nums);
                return $nums;
            }
        }
    }

    // 获取小房间的名字
    static function smallroomname($id){
        switch ($id) {
            case '0':
                $name = "开始位置";
                break;
            case '1':
                $name = "媒体中心";
                break;
            case '2':
                $name = "娱乐中心";
                break;
            case '3':
                $name = "旅游中心";
                break;
            case '4':
                $name = "康养中心";
                break;
            case '5':
                $name = "研发中心";
                break;
            case '6':
                $name = "金融中心";
                break;
            case '7':
                $name = "能源中心";
                break;
            case '8':
                $name = "购物中心";
                break;
          
        }
        // var_dump($id,$name);
        $result = isset($name) ? $name : '';
        return $result;
    }

    // 获取我的投注记录
    static function getmytzinfo($uid){
        $info['allcoin']=(string)Db::table("wa_touzhu")->where(['uid'=>$uid,'ifyouxiao'=>1])->sum("coin");
        $info['reward']=(string)Db::table("wa_touzhu")->where(['uid'=>$uid,'ifyouxiao'=>1,'status'=>1])->sum("rewards");
        $info['list']=Db::table("wa_touzhu")->where(['uid'=>$uid,'ifyouxiao'=>1])->orderByDesc('id',true)->limit(60)->get();
        return $info;
    }

    // 获取奖池 昨日今日 销毁和能量总计
    static function getjiangchirecord($roomid){
        $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $today_end = mktime(23, 59, 59, date('m'), date('d'), date('Y'));

        $yesterday = date('d') - 1; 
        $yesterday_start =  mktime(0, 0, 0, date('m'), $yesterday, date('Y'));
        $yesterday_end = mktime(23, 59, 59, date('m'), $yesterday, date('Y'));

        $today_xiaohui = Db::table("wa_jiangchi")->where("addtime",'>',$today_start)->where("addtime",'<',$today_end)->where("roomid",$roomid)->sum("xiaohui");
        $yesterday_xiaohui = Db::table("wa_jiangchi")->where("addtime",'>',$yesterday_start)->where("addtime",'<',$yesterday_end)->where("roomid",$roomid)->sum("xiaohui");
        $today_nengliang = Db::table("wa_jiangchi")->where("addtime",'>',$today_start)->where("addtime",'<',$today_end)->where("roomid",$roomid)->sum("nengliang");
        $yesterday_nengliang = Db::table("wa_jiangchi")->where("addtime",'>',$yesterday_start)->where("addtime",'<',$yesterday_end)->where("roomid",$roomid)->sum("nengliang");

        $allxiaohui = Db::table("wa_jiangchi")->where("roomid",$roomid)->sum("xiaohui");
        $allnengliang = Db::table("wa_jiangchi")->where("roomid",$roomid)->sum("nengliang");


        $rs['today_xiaohui']=(string)round($today_xiaohui,2);
        $rs['yesterday_xiaohui']=(string)round($yesterday_xiaohui,2);
        $rs['today_nengliang']=(string)round($today_nengliang,2);
        $rs['yesterday_nengliang']=(string)round($yesterday_nengliang,2);
        $rs['allxiaohui']=(string)round($yesterday_nengliang,2);
        $rs['allnengliang']=(string)round($yesterday_nengliang,2);
        return $rs;

    }

}